﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace zykControl
{
    public partial class FrmBoxIn : Form
    {
        bool mIsOK;
        bool mModeC;
        Func<string, bool>? mModeCFunc;
        Func<string, Task<bool>>? mModeCFuncTask;

        private async void Command1_Click(object? eventSender, EventArgs eventArgs)
        {
            //逻辑绕了弯,但是意思很简单. 就是如果是模式C,就调用检查函数,检查通过才关闭对话框,其它模式则直接关闭对话框
            if (!mModeC || (mModeCFunc?.Invoke(Text1.Text) ?? true) && (mModeCFuncTask == null || (await mModeCFuncTask.Invoke(Text1.Text))))
            {
                mIsOK = true;
                Close();
            }
        }

        private void Command2_Click(object? eventSender, EventArgs eventArgs)
        {
            Close();
        }

        public static bool ShowA(string DefaultText, ref string ReturnText, string theCaption = "", bool PwdStyle = false)
        {
            var dI = new FrmBoxIn();
            dI.Text1.Text = DefaultText;
            dI.Label1.Text = theCaption;
            if (PwdStyle == true)
                dI.Text1.PasswordChar = '*';
            else
                dI.Text1.PasswordChar = default;
            dI.mIsOK = false;
            dI.mModeC = false;
            dI.mModeCFunc = null;
            dI.Text1.Focus();
            dI.ShowDialog();
            if (dI.mIsOK == true)
                ReturnText = dI.Text1.Text;
            else
                ReturnText = "";
            return dI.mIsOK;
        }

        /// <summary>
        /// 同ShowA,但是取消对话框时返回Nothing
        /// </summary>
        /// <param name="DefaultText"></param>
        /// <param name="theCaption"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string? ShowB(string DefaultText, string theCaption = "")
        {
            var dI = new FrmBoxIn();
            dI.Text1.Text = DefaultText;
            dI.Label1.Text = theCaption;
            dI.Text1.PasswordChar = default;
            dI.mIsOK = false;
            dI.mModeC = false;
            dI.mModeCFunc = null;
            dI.Text1.Focus();
            dI.ShowDialog();
            if (dI.mIsOK)
                return dI.Text1.Text;
            else
                return null;
        }

        /// <summary>
        /// 同ShowB,参数theFunc用于在对话框关闭之前对输入的内容做检查,True表示检查通过
        /// </summary>
        /// <param name="DefaultText"></param>
        /// <param name="thefunc"></param>
        /// <param name="theCaption"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string? ShowC(string theCaption = "", string DefaultText = "", Func<string, bool>? thefunc = null)
        {
            var me = new FrmBoxIn();
            me.Text1.Text = DefaultText;
            me.Label1.Text = theCaption;
            me.Text1.PasswordChar = default;
            me.mIsOK = false;
            me.mModeC = true;
            me.mModeCFunc = thefunc;
            me.Text1.Focus();
            me.ShowDialog();
            if (me.mIsOK)
                return me.Text1.Text;
            else
                return null;
        }

        /// <summary>
        /// 同ShowB,参数theFunc用于在对话框关闭之前对输入的内容做检查,True表示检查通过
        /// </summary>
        /// <param name="DefaultText"></param>
        /// <param name="thefunc"></param>
        /// <param name="theCaption"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string? ShowC(string theCaption = "", string DefaultText = "", Func<string, Task<bool>>? thefunc = null)
        {
            var me = new FrmBoxIn();
            me.Text1.Text = DefaultText;
            me.Label1.Text = theCaption;
            me.Text1.PasswordChar = default;
            me.mIsOK = false;
            me.mModeC = true;
            me.mModeCFuncTask = thefunc;
            me.Text1.Focus();
            me.ShowDialog();
            if (me.mIsOK)
                return me.Text1.Text;
            else
                return null;
        }

        private void FrmBoxIn_Shown(object? sender, EventArgs e)
        {
            Text1.Focus();
        }
    }
}